﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace TMS.ServiceProvider.CapitalTransportationSolutions.ShipRite
{
    public class AccesorialProvider : ShipRiteBase
    {
        public static AccessorialList GetAccesorialList()
        {
            AccessorialList accessorials = null;

            XmlReaderSettings settings = new XmlReaderSettings() { IgnoreWhitespace = true };

            XmlReader xmlReader = FetchXmlReaderFromUrl(AccesorialRequestUrl, settings);

            if (xmlReader != null)
            {
                accessorials = Deserialize(xmlReader);
            }

            if (accessorials != null)
            {
                string strValue = accessorials.Error;
                if (!String.IsNullOrEmpty(strValue) && strValue != "0")
                {
                    throw new Exception(strValue);
                }
            }
            return accessorials;
        }

        public static AccessorialList Deserialize(XmlReader xmlReader)
        {
            AccessorialList accessorials = null;

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(AccessorialList));

            if (xmlSerializer.CanDeserialize(xmlReader))
            {
                accessorials = (AccessorialList)xmlSerializer.Deserialize(xmlReader);
            }

            return accessorials;
        }
    }
}
